
var tagRules = {


	'.infoBumperCancel': function( element ) {
		element.onclick = function() {
			new Effect.BlindUp( element.parentNode.parentNode, {duration: .2} );
			Element.remove( element.parentNode );
		}
	},

	'.tagDelete': function( element ) {
		element.onclick = function() {
			var id = element.getAttribute( 'for' );
			
			if( id && $('infoBumper'+id).style.display == 'none' ) {
				var span = document.createElement( 'span' );
				span.className = 'infoBumper';
				
				new Insertion.Top( span, 'Are you sure you want to <b>delete</b> this tag?' );
				
				var button = document.createElement( 'input' );
				button.type = 'button';
				button.value = 'Delete';
				button.className = 'tagDeleteNow';
				button.setAttribute( 'for', id );
				button.style.marginLeft = '1em';
				button.style.marginRight = '1em';
				
				var href = document.createElement( 'a' );
				href.href = '#';
				href.innerHTML = 'Cancel';
				href.className = 'infoBumperCancel';
				href.id = 'infoBumperCancel'+id;
				
				span.appendChild( button );
				span.appendChild( href );
				
				$('infoBumper'+id).appendChild( span );

				Behaviour.apply();

				new Effect.BlindDown( 'infoBumper'+id, {duration: .2} );
			}
		}
	},
	
	'.tagDeleteNow': function( element ) {
		element.onclick = function() {
			
			var id = element.getAttribute( 'for' );
			
			if( id ) {
	
				var spinner = document.createElement( 'img' );
				spinner.id = 'tageEditSpinner';
				spinner.src = BASEURL.replace(
					new RegExp( 'index.php' ),
					'images/spinner.gif'
				);

				$('infoBumper'+id).appendChild( spinner );
				
				var url = BASEURL+'/'+MKEY+'/deleteTag';
				
				new Ajax.Updater(
					'ajaxError',
					url,
					{parameters: 'id='+id, evalScripts: true, method: 'post'}
				);
				
				window.deleteTag = function( data ) {

					if( data.uid ) {
					
						Element.remove( spinner );
						$('infoBumperCancel'+id).onclick();
											
						new Effect.Puff( 'tagContainer'+id );
						actionMessage( 'Tag '+id+' deleted!' );
					} else {
						actionMessage( 'Could not remove tag!' );
					}

				}
			}			
			

		}
	},
	
	'#showNewTag': function( element ) {
		element.onclick = function() {
			if( $('newTagContainer').style.display == 'none' ) {
				new Effect.BlindDown( 'newTagContainer', {duration:.2} );
			} else {
				new Effect.BlindUp( 'newTagContainer', {duration:.2} );
			}
		}
	},
	
	'.tagEdit': function( element ) {
		element.onclick = function() {
			var id = element.getAttribute( 'for' );

			if( id ) {

				if( $('tagNameContainer'+id) ) {

					var span = document.createElement( 'span' );
					span.id = 'tagEditContainer'+id;
					

					var input = document.createElement( 'input' );
					input.value = $('tagNameContainer'+id).innerHTML;
					input.className = 'tagEditNameContainer';
					input.id = 'tagEditNameContainer'+id;
					input.setAttribute( 'for', id );
					
					var button = document.createElement( 'input' );
					button.type = 'button';
					button.value = 'Save';
					button.setAttribute( 'for', id );
					button.className = 'tagEditSave';
					button.id = 'tagEditSave'+id;

					
					span.appendChild( input );
					span.appendChild( button );
					
					$('tagNameContainer'+id).parentNode.replaceChild(
						span,
						$('tagNameContainer'+id)
					);
				} else {
				
					var span = document.createElement( 'span' );
					span.id = 'tagNameContainer'+id;
					span.innerHTML = $('tagEditNameContainer'+id).value;
					
					$('tagEditContainer'+id).parentNode.replaceChild(
						span,
						$('tagEditContainer'+id)
					);
				
				}
				Behaviour.apply();
			}
		}
	},
	
	'.tagEditSave': function( element ) {
		element.onclick = function() {
			
			var id = element.getAttribute( 'for' );
			var name = $('tagEditNameContainer'+id).value;
			if( id && name ) {

				var img = document.createElement( 'img' );
				img.src = BASEURL.replace(
					new RegExp( 'index.php' ),
					'images/spinner.gif'
				);
				
				$('tagEditContainer'+id ).appendChild( img );
								
				var url = BASEURL+'/'+MKEY+'/editTag';
				new Ajax.Updater(
					'ajaxError',
					url,
					{parameters:'tagID='+id+'&tagName='+name, method:'post',
						evalScripts:true}
				);
				
				window.editTag = function( data ) {
				
					if( data.uid ) {
					
						actionMessage( 'Tag saved!' );
	
						var span = document.createElement( 'span' );
						span.id = 'tagNameContainer'+id;
						span.innerHTML = $('tagEditNameContainer'+id).value;
						
					} else {
						actionMessage( 'Could not save tag!' );
					}

					$('tagEditContainer'+id).parentNode.replaceChild(
						span,
						$('tagEditContainer'+id)
					);
					Behaviour.apply();

				}
			}
		}
	}
};

Behaviour.register( tagRules );